#!/bin/bash

unset TMUX
work_dir="&{path_prefix}"
tmux_name="&{tmux_name}"
listen_addr="&{listen_addr}"
ipmi_port="&{ipmi_port}"
cmd="&{ipmi_sim} -c &{ipmi_config_file}"

cd "$work_dir"

help() {
    echo "usage: $0 <start|stop|restart|startvm|stopvm> ipmiuser ipmipassword"
    exit 1
}


if [ $# -lt 3 ]; then
    help
else
    ACT="$1"
    ipmiusr="$2"
    ipmipass="$3"
fi

case "$ACT" in
    start)
        ./tmux-cmd "$tmux_name" start "$cmd"
        ;;
    stop)
        ./tmux-cmd "$tmux_name" stop
        ;;
    restart)
        ./tmux-cmd "$tmux_name" restart
        ;;
    startvm)
        ipmitool -I lanplus -U "$ipmiusr" -P "$ipmipass" -H $listen_addr -p $ipmi_port chassis power on
        ;;
    stopvm)
        ipmitool -I lanplus -U "$ipmiusr" -P "$ipmipass" -H $listen_addr -p $ipmi_port chassis power off
        ;;
    *)
        help
esac
